package cn.sstech.member.core.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TimeUtil {

	static Logger logger = LoggerFactory.getLogger(TimeUtil.class);
	
	public static String getCurrentDateTime(){
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		dateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
		
		return dateFormat.format(new Date());
	}
	
	public static String getCurrentDate(){
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		dateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
		
		return dateFormat.format(new Date());
	}
	
	public static String getDateStr(Date date){
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		dateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
		
		return dateFormat.format(date);
	}
	
	public static Date getDateTimeByStr(String timeDate) throws ParseException{
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		dateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
		
		return dateFormat.parse(timeDate);
	}
	
	public static Date getDateByStr(String timeDate) throws ParseException{
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		dateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
		
		return dateFormat.parse(timeDate);
	}

	public static Date formateTimeStr(String pTime) {
		Date time = null;
		try {
			if(time == null){
				SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				dateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
				time = dateFormat.parse(pTime);
			}
		} catch (Exception e) {
			logger.debug("时间转换1");
		}
		
		try {
			if(time == null){
				SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
				dateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
				time = dateFormat.parse(pTime);
			}
		} catch (Exception e) {
			logger.debug("时间转换2");
		}
		
		try {
			if(time == null){
				SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH");
				dateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
				time = dateFormat.parse(pTime);
			}
		} catch (Exception e) {
			logger.debug("时间转换3");
		}
		
		try {
			if(time == null){
				SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
				dateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
				time = dateFormat.parse(pTime);
			}
		} catch (Exception e) {
			logger.debug("时间转换4");
		}
		
		if(time!=null){
			return time;
		}
		else{
			return null;
		}
		
	}
	
	public static Date addDays(Date date,Integer days){
		if(date!=null && days!=null){
			Calendar cal = Calendar.getInstance();
			cal.setTime(date);
			cal.add(Calendar.DAY_OF_YEAR, days);
			return cal.getTime();
		}
		return null;
	}
}
